<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D旋转</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            background-color: pink;
            overflow: hidden;
        }

        .wrapper {
            position: relative;
            left: 40%;
            top: 150px;
            /* transform: translateX(-50%); */
            width: 400px;
            text-align: center;
        }

        .wrapper form {
            padding: 40px 10px;
            background-color: white;
        }

        .wrapper h2 {
            color: #1f2029;
            pointer-events: none;
        }

        .wrapper input {
            width: 80%;
            height: 50px;
            margin-top: 30px;
            border: none;
            outline: none;
            background-color: #4E495D;
            padding: 0 10px;
            box-shadow: 0 4px 8px rgba(31, 32, 41, .5);
            border-radius: 8px;
            font-size: 18px;
            color: white;
        }

        .wrapper .btn {
            background-color: #981E61;
            color: white;
            font-size: 24px;
            font-weight: 600;
            cursor: pointer;
        }

        .wrapper p {
            margin-top: 10px;
            font-size: 14px;
            height: 20px;
            color: gray;
            pointer-events: none;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <form class="login">
            <h2>登录界面</h2>
            <input type="text" placeholder="用户名">
            <p class="user"></p>
            <input type="password" placeholder="密码">
            <p class="psw"></p>
            <input type="submit" class="btn" value="登 录">
        </form>
    </div>

    <script>
        const wraper = document.querySelector('.wrapper')
        const userInput = document.querySelector('input[type="text"]')
        const passwordInput = document.querySelector('input[type="password"]')
        const submitEle = document.querySelector('input[type="submit"]')
        const h2Ele = document.querySelector('h2')
        const userP = document.querySelector('.user')
        const pswP = document.querySelector('.psw')

        userInput.onmousedown = function (e) {
            e = e || window.event
            e.stopPropagation()
        }
        passwordInput.onmousedown = function (e) {
            e = e || window.event
            e.stopPropagation()
        }
        submitEle.onmousedown = function (e) {
            e = e || window.event
            e.stopPropagation()
        }
        wraper.onmousedown = function (e) {
            e = e || window.event
            let l = e.offsetX
            let t = e.offsetY

            document.onmousemove = function (e) {
                e = e || window.event
                let x = e.clientX - l
                let y = e.clientY - t

                //邊界检查
                if (x < 0) x = 0
                if (x > window.innerWidth - wraper.offsetWidth)
                    x = window.innerWidth - wraper.offsetWidth
                if (y < 0) y = 0
                if (y > window.innerHeight - wraper.offsetHeight)
                    y = window.innerHeight - wraper.offsetHeight


                wraper.style.left = x + 'px'
                wraper.style.top = y + 'px'

            }
            document.onmouseup = function () {
                document.onmousemove = null

            }
        }

        submitEle.onclick = function (e) {
            e = e || window.event
            e.preventDefault()

            let isUserNull = checkUserNull()
            let isPswNull = checkPasswordNull()
            if(isUserNull || isPswNull){
               return  
            }
            if(!checkUser()){
                return
            }
            alert('登录成功！')

        }

        function checkUserNull() {
            if (userInput.value == '') {
                userP.innerHTML = '用戶名不能為空'
                return true
            }else{
                userP.innerHTML = ''
                return false
            }
        }
        function checkPasswordNull() {
            if (passwordInput.value == '') {
                pswP.innerHTML = '密码不能为空'
                return true
            }else{
                pswP.innerHTML = ''
                return false
            }
        }

        function checkUser(){
            if(userInput.value == 'admin' && passwordInput.value == '123456'){
                pswP.innerHTML = ''
                return true
            }else{
                pswP.innerHTML = '用户名或密码出错'
                return false
            }
        }

    </script>
</body>

</html>